<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("comctrls.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Vehicle_Expense_Edit extends Page
{
       public $cmbVendor = null;
       public $lblVendor = null;
       public $JTJavaScript6 = null;
       public $JTJavaScript5 = null;
       public $JTJavaScript4 = null;
       public $JTJavaScript9 = null;
       public $JTJavaScript8 = null;
       public $JTJavaScript7 = null;
       public $JTJavaScript3 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $Label12 = null;
       public $lblamt = null;
       public $JSValidate = null;
       public $hfError = null;
       public $btnShowDesc = null;
       public $btnShowAccount = null;
       public $btnShowType = null;
       public $cmbAccount = null;
       public $lblAccount = null;
       public $edAmount = null;
       public $lblAmount = null;
       public $chkBusiness = null;
       public $memoComments = null;
       public $lblComments = null;
       public $cmbDescription = null;
       public $lblDescription = null;
       public $dpDate = null;
       public $lblDate = null;
       public $cmbType = null;
       public $lblType = null;
       public $btnCancel = null;
       public $btnUpdate = null;
       public $JTDivDesc = null;
       public $JTGroupBox3 = null;
       public $btnAddDesc = null;
       public $btnDelDesc = null;
       public $edDesc = null;
       public $listDesc = null;
       public $JTDivAccount = null;
       public $JTGroupBox2 = null;
       public $btnAddAccount = null;
       public $btnDelAccount = null;
       public $edAccount = null;
       public $listAccount = null;
       public $JTDivType = null;
       public $JTGroupBox1 = null;
       public $btnAddType = null;
       public $btnDelType = null;
       public $edType = null;
       public $listType = null;
       public $compid = null;
       public $imgBack = null;
       public $Image3 = null;
       public $hiddenExpense = null;
       public $hiddenVeh = null;
       public $JTSiteTheme1 = null;
       function edAmountJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       //accept only numbers
       var numero = document.getElementById("edAmount").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edAmount").value = numero;
       <?php

       }

       function edAmountJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edAmount','lblamt');
       <?php

       }

        function btnDelDescJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var DescIndex = document.getElementById('listDesc').selectedIndex;
       var DescValue = document.getElementById('listDesc')[DescIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxDesc3(DescValue,comp);
       <?php

       }

       function btnAddDescJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var adddesc = document.getElementById('edDesc').value;
       var comp = document.getElementById('compid').value;
       AjaxDesc2(adddesc,comp);
       document.Vehicle_Expense_Edit.edDesc.value = "";
       <?php

       }

       function btnShowDescJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivDesc').Show()
       document.Vehicle_Expense_Edit.edDesc.value = "";
       <?php

       }


       function btnDelAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var AccountIndex = document.getElementById('listAccount').selectedIndex;
       var AccountValue = document.getElementById('listAccount')[AccountIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxAccount3(AccountValue,comp);
       <?php

       }

       function btnAddAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addaccount = document.getElementById('edAccount').value;
       var comp = document.getElementById('compid').value;
       AjaxAccount2(addaccount,comp);
       document.Vehicle_Expense_Edit.edAccount.value = "";
       <?php

       }

       function btnShowAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivAccount').Show();
       document.Vehicle_Expense_Edit.edAccount.value = "";
       <?php

       }

       function btnDelTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var TypeIndex = document.getElementById('listType').selectedIndex;
       var TypeValue = document.getElementById('listType')[TypeIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxType3(TypeValue,comp);
       <?php

       }

       function btnAddTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addtype = document.getElementById('edType').value;
       var comp = document.getElementById('compid').value;
       AjaxType2(addtype,comp);
       document.Vehicle_Expense_Edit.edType.value = "";
       <?php

       }

       function btnShowTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivType').Show();
       document.Vehicle_Expense_Edit.edType.value = "";
       <?php

       }


       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function btnCancelClick($sender, $params)
       {
       redirect('details_emission2.php?layer=1&veh='.$this->hiddenVeh->getValue());

       }


       function btnUpdateClick($sender, $params)
       {
       $this->validateRequired($this->edAmount->Text, 'lblamt');
       $error = $this->hfError->getValue();
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {

       $type = $this->cmbType->getItemIndex();
       $res_type = mysql_query("select * from lookup_expense_tbl where company_id='".$_SESSION['company_id']."' AND id='".$type."'");
       $row_type = mysql_fetch_array($res_type);

       $query1 = "select * from veh_expense_tbl where vehicle_id='".$this->hiddenVeh->getValue()."'and payment_id='".$this->hiddenExpense->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());
       $row = mysql_fetch_array($result);

       $desc = $this->cmbDescription->getItemIndex();
       $res_desc = mysql_query("select * from lookup_expense_tbl where company_id= '".$_SESSION['company_id']."' AND id='".$desc."'");
       $row_desc = mysql_fetch_array($res_desc);

       $query_update = "UPDATE veh_expense_tbl SET
       type='".$row_type['value']."',
       type_id='".$this->cmbType->getItemIndex()."',
       amount='".$this->edAmount->Text."',
       date='".$this->dpDate->Text."',
       account='".$this->cmbAccount->getItemIndex()."',
       description='".$row_desc['value']."',
       desc_id='".$this->cmbDescription->getItemindex()."',
       comments='".$this->memoComments->Text."',
       vendor = '".$this->cmbVendor->getItemIndex()."'
       WHERE vehicle_id='".$this->hiddenVeh->getValue()."'and payment_id ='".$this->hiddenExpense->getValue()."'";
       mysql_query($query_update) or die(mysql_error());


       mysql_query("UPDATE rou_expense_tbl SET
       amount = '".$this->edAmount->Text."',
       type = '".$this->cmbType->getItemIndex()."',
       account = '".$this->cmbAccount->getItemIndex()."',
       description = '".$this->memoComments->Text."',
       date = '".$this->dpDate->Text."'
       WHERE expense_id='".$row['route_payment_id']."'")
       or die ('UPDATE JOB -' . mysql_error());


       redirect('details_emission2.php?layer=1&veh='.$this->hiddenVeh->getValue());
       }

       }

       function validateRequired($value, $lbl)
       {
          if($value == '' || $value == null)
          {
             $this->$lbl->Caption = '&darr; This field is required &darr;';
             $this->hfError->setValue('1');
          }
       }
       function Vehicle_Expense_EditCreate($sender, $params)
       {
       $this->hfError->setValue('0');
       $this->lblamt->Caption = '';

       $this->compid->Value = $_SESSION['company_id'];
       $veh_id = $_GET['veh'];
       $veh_expense = $_GET['expense'];
       $this->hiddenVeh->setValue($veh_id);
       $this->hiddenExpense->setValue($veh_expense);

       if($_SESSION['access_vehicle']==1)
       {
       $this->btnUpdate->Visible=false;
       $this->btnCancel->Visible=false;
       }

       if($_SESSION['access_vehicle']==2)
       {
       $this->btnUpdate->Visible=true;
       $this->btnCancel->Visible=true;
       }

       $query1 = "select * from veh_expense_tbl where vehicle_id='".$this->hiddenVeh->getValue()."'and payment_id='".$this->hiddenExpense->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());
       $row = mysql_fetch_array($result);

       $this->edAmount->Text = $row['amount'];
       $this->dpDate->Text = $row['date'];
       $this->memoComments->Text = $row['comments'];


      $this->imgBack->setLink('details_emission2.php?layer=1&veh='.$this->hiddenVeh->getValue());

      $this->cmbType->ItemIndex = $row['type_id'];
      $this->cmbType->AddItem('--Select--',null,'--Select--');
      $result_type = mysql_query("select * from lookup_expense_tbl where company_id= '".$_SESSION['company_id']."' AND category='types'");

      while($row_type = mysql_fetch_array($result_type))
      {
        $this->cmbType->AddItem($row_type['value'], null, $row_type['id']);
      }

      $this->cmbVendor->Clear();
       $this->cmbVendor->ItemIndex = $row['vendor'];
       $result_vendor = mysql_query("select * from vendor_tbl where company_id='".$_SESSION['company_id']."'");

       while($row_vendor = mysql_fetch_array($result_vendor))
       {
            $this->cmbVendor->AddItem($row_vendor['vendor_name'], null, $row_vendor['vendor_name']);
       }
       $this->cmbVendor->AddItem('Others', null, 'Others');

      //list type
      $this->listType->Clear();
      $result_t = mysql_query("select * from lookup_expense_tbl where company_id= '".$_SESSION['company_id']."' AND category='types'");

      while($row_t = mysql_fetch_array($result_t))
      {
        $this->listType->AddItem($row_t['value'], null, $row_t['id']);
      }

      $this->cmbDescription->ItemIndex = $row['desc_id'];
      $this->cmbDescription->AddItem('--Select--',null,'--Select--');
      $result_desc = mysql_query("select * from lookup_expense_tbl where company_id= '".$_SESSION['company_id']."' AND category='descriptions'");

      while($row_desc = mysql_fetch_array($result_desc))
      {
        $this->cmbDescription->AddItem($row_desc['value'], null, $row_desc['id']);
      }
      // list Description
      $this->listDesc->Clear();
      $result_d = mysql_query("select * from lookup_expense_tbl where company_id= '".$_SESSION['company_id']."' AND category='descriptions'");

      while($row_d = mysql_fetch_array($result_d))
      {
        $this->listDesc->AddItem($row_d['value'], null, $row_d['id']);
      }


      $this->cmbAccount->ItemIndex = $row['account'];
      $this->cmbAccount->AddItem('--Select--',null,'--Select--');
      $result_account = mysql_query("select * from lookup_expense_tbl where company_id= '".$_SESSION['company_id']."' AND category='accounts'");

      while($row_account = mysql_fetch_array($result_account))
      {
        $this->cmbAccount->AddItem($row_account['value'], null, $row_account['id']);
      }
       //list account
       $this->listAccount->Clear();
      $result_a = mysql_query("select * from lookup_expense_tbl where company_id= '".$_SESSION['company_id']."' AND category='accounts'");

      while($row_a = mysql_fetch_array($result_a))
      {
        $this->listAccount->AddItem($row_a['value'], null, $row_a['id']);
      }

       }

}

global $application;

global $Vehicle_Expense_Edit;

//Creates the form
$Vehicle_Expense_Edit=new Vehicle_Expense_Edit($application);

//Read from resource file
$Vehicle_Expense_Edit->loadResource(__FILE__);

//Shows the form
$Vehicle_Expense_Edit->show();

?>